package com.wangzefeng.shop.controller.base;

import com.wangzefeng.shop.constant.CommonConstants;
import com.wangzefeng.shop.entity.SysUser;
import com.wangzefeng.shop.pojo.CommonResult;
import com.wangzefeng.shop.service.SysUserService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @Auther: wangzefeng
 * @Date: 2021/3/26 19:46
 * @Description:
 */
@RestController
@Slf4j
public class BaseController {

    @Autowired
    SysUserService sysUserService;

    @Resource
    public HttpServletRequest request;

    @Resource
    public HttpServletResponse response;


    public CommonResult getCurrentUserInfo(){
        String token = request.getHeader("Authorization");
        Claims claims = Jwts.parser().setSigningKey(CommonConstants.SIGNING_KEY).parseClaimsJws(token.replace("Bearer ", "")).getBody();
        String subject = claims.getSubject();
        String username = subject.split("-")[0];
        SysUser data = sysUserService.selectOneByUsername(username);
        CommonResult successResult = CommonResult.getSuccessResult();
        successResult.setData(data);
        return successResult;
    }
}
